/// CreateTime 2020/7/27 15:26
/// CreateUser chen 
/// ProjectName flutter_elf
import 'package:flutter_elf/config/component_index.dart';
import 'dart:convert';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:dio/dio.dart';

import 'package:cookie_jar/cookie_jar.dart';
class HttpRequest {
  Dio _dio;
  static final HttpRequest instance = HttpRequest._internal();


  factory HttpRequest() => instance;

  HttpRequest._internal() {
    initDio();
  }

  void initDio() {
    // 配置dio实例
    BaseOptions options = BaseOptions(
      connectTimeout: 15000,
      receiveTimeout: 15000,
      contentType: 'application/json',
    );
    //设置请求头
    options.headers = {"timeoutMs": 15000, "Authorization": null};
    _dio = new Dio(options);
    settingInterceptor();
  }

  settingInterceptor(){
//    _dio.interceptors
//        .add(InterceptorsWrapper(onError: onHttpError, onRequest: onRequest));
    _dio.interceptors.add(CookieManager(CookieJar()));
  }


  Future netFetch(String path, params,
      {String method = Method.POST,
        Options option,
        }) async {
    Options options = option ?? Options();
    Response response;
    try {
      response = await _dio.request(path,
          data: method != Method.GET ? params : null,
          queryParameters: method == Method.GET ? params : null,
          options: options..method = method);
    } on DioError catch (e) {
      return Future.error(e.error);
    }
    return response.data;
  }

}